package routers

import (
	"gateway-api/controllers"
	v1 "gateway-api/controllers/v1"
	v2 "gateway-api/controllers/v2"
	"gateway-api/middlewares"
	"gateway-api/tools"

	"github.com/gin-gonic/gin"
)

func ApiRouters(r *gin.Engine) {

	// 前端不区分版本接口之非权限路由
	unauthGroupRouter := r.Group("/api/frontend")
	{
		unauthGroupRouter.GET("/es/index", controllers.ElasticSearchController{}.Index)

		//base64Captcha
		unauthGroupRouter.GET("/captcha", func(ctx *gin.Context) {
			id, b64s, err := tools.MakeCaptcha()
			if err != nil {
				ctx.JSON(200, gin.H{
					"code": 400,
					"data": nil,
					"msg":  err.Error(),
				})
				return
			}

			ctx.JSON(200, gin.H{
				"code": 200,
				"data": map[string]interface{}{
					"captchaId":    id,
					"captchaImage": b64s,
				},
				"msg": "",
			})
		})

		//生成二维码
		unauthGroupRouter.GET("/qrcode", func(ctx *gin.Context) {
			// qrcode, err := tools.GenerateQrcode(ctx, "https://www.itrident.com.cn", "string")
			qrcode, err := tools.GenerateQrcode(ctx, "https://www.itrident.com.cn", "image")
			if err != nil {
				ctx.String(400, err.Error())
				return
			}

			ctx.String(200, qrcode)
		})

		// -------------------- DTM路由开始 --------------------
		// +++++++++++++++++++ Saga事务模式开始 +++++++++++++++++++
		// Saga HTTP 方式
		unauthGroupRouter.GET("saga/sagaHttp", controllers.SagaController{}.SagaHttp)

		// Saga GRPC 方式
		unauthGroupRouter.GET("saga/sagaGrpc", controllers.SagaController{}.SagaGrpc)

		// Saga 转入
		unauthGroupRouter.POST("saga/transIn", controllers.SagaController{}.TransIn)

		// Saga 补偿转入
		unauthGroupRouter.POST("saga/transInRevert", controllers.SagaController{}.TransInRevert)

		// Saga 转出
		unauthGroupRouter.POST("saga/transOut", controllers.SagaController{}.TransOut)

		// Saga 补偿转出
		unauthGroupRouter.POST("saga/transOutRevert", controllers.SagaController{}.TransOutRevert)
		// +++++++++++++++++++ Saga事务模式结束 +++++++++++++++++++

		// +++++++++++++++++++ Tcc事务模式开始 +++++++++++++++++++
		// Tcc HTTP 方式
		unauthGroupRouter.GET("tcc/tccHttp", controllers.TccController{}.TccHttp)

		// Tcc GRPC 方式
		unauthGroupRouter.GET("tcc/tccGrpc", controllers.TccController{}.TccGrpc)

		// Tcc 尝试转入
		unauthGroupRouter.POST("tcc/transInTry", controllers.TccController{}.TransInTry)

		// Tcc 确认转入
		unauthGroupRouter.POST("tcc/transInConfirm", controllers.TccController{}.TransInConfirm)

		// Tcc 取消转入
		unauthGroupRouter.POST("tcc/transInCancel", controllers.TccController{}.TransInCancel)

		// Tcc 尝试转出
		unauthGroupRouter.POST("tcc/transOutTry", controllers.TccController{}.TransOutTry)

		// Tcc 确认转出
		unauthGroupRouter.POST("tcc/transOutConfirm", controllers.TccController{}.TransOutConfirm)

		// Tcc 取消转出
		unauthGroupRouter.POST("tcc/transOutCancel", controllers.TccController{}.TransOutCancel)
		// +++++++++++++++++++ Tcc事务模式结束 +++++++++++++++++++

		// ++++++++++++++++++++ XA事务模式开始 +++++++++++++++++++
		// XA HTTP 方式
		unauthGroupRouter.GET("xa/xaHttp", controllers.XaController{}.XaHttp)

		// XA 转出
		unauthGroupRouter.POST("xa/transOutXaGorm", controllers.XaController{}.TransOutXaGorm)

		// XA 转入
		unauthGroupRouter.POST("xa/transInXa", controllers.XaController{}.TransInXa)
		// ++++++++++++++++++++ XA事务模式结束 +++++++++++++++++++

		// ++++++++++++++++ WorkflowSaga事务模式开始 +++++++++++++
		// WorkflowSaga HTTP 方式
		unauthGroupRouter.GET("workflowSaga/wSagaHttp", controllers.WorkflowSagaController{}.WSagaHttp)

		// WorkflowSaga GRPC 方式
		unauthGroupRouter.GET("workflowSaga/wSagaGrpc", controllers.WorkflowSagaController{}.WSagaGrpc)

		unauthGroupRouter.POST("workflowSaga/resume", controllers.WorkflowSagaController{}.Resume)

		// WorkflowSaga 转出
		unauthGroupRouter.POST("workflowSaga/transOut", controllers.WorkflowSagaController{}.TransOut)

		// WorkflowSaga 补偿转出
		unauthGroupRouter.POST("workflowSaga/transOutRevert", controllers.WorkflowSagaController{}.TransOutRevert)

		// WorkflowSaga 转出
		unauthGroupRouter.POST("workflowSaga/transIn", controllers.WorkflowSagaController{}.TransIn)

		// WorkflowSaga 补偿转入
		unauthGroupRouter.POST("workflowSaga/transInRevert", controllers.WorkflowSagaController{}.TransInRevert)
		// ++++++++++++++++ WorkflowSaga事务模式结束 +++++++++++++
		// ----------------------------------------------------
		// -------------------- DTM路由结束 --------------------
		// ----------------------------------------------------

		// ----------------------------------------------------
		// ----------------- RabbitMQ路由开始  -----------------
		// ----------------------------------------------------
		// 普通队列
		unauthGroupRouter.GET("rabbitMQ/simpleProducer", controllers.RabbitMQController{}.SimpleProducer)

		// 工作队列
		unauthGroupRouter.GET("rabbitMQ/workProducer", controllers.RabbitMQController{}.WorkProducer)

		// 广播队列
		unauthGroupRouter.GET("rabbitMQ/fanoutProducer", controllers.RabbitMQController{}.FanoutProducer)

		// 定向路由类型消息
		unauthGroupRouter.GET("rabbitMQ/directProducer", controllers.RabbitMQController{}.DirectProducer)

		// 主题类型交换机
		unauthGroupRouter.GET("rabbitMQ/topicProducer", controllers.RabbitMQController{}.TopicProducer)

		// 重试案例
		unauthGroupRouter.GET("rabbitMQ/retryProducer", controllers.RabbitMQController{}.RetryProducer)

		// 惰性队列
		unauthGroupRouter.GET("rabbitMQ/lazyProducer", controllers.RabbitMQController{}.LazyProducer)

		// 幂等性案列
		unauthGroupRouter.GET("rabbitMQ/idempotentProducer", controllers.RabbitMQController{}.IdempotentProducer)

		// 死信队列
		unauthGroupRouter.GET("rabbitMQ/deadProducer", controllers.RabbitMQController{}.DeadProducer)

		// 延迟消息
		unauthGroupRouter.GET("rabbitMQ/delayProducer", controllers.RabbitMQController{}.DelayProducer)

		// 消息过期
		unauthGroupRouter.GET("rabbitMQ/messageExpiredProducer", controllers.RabbitMQController{}.MessageExpiredProducer)

		// 队列过期
		unauthGroupRouter.GET("rabbitMQ/queueExpiredProducer", controllers.RabbitMQController{}.QueueExpiredProducer)

		// 备用交换机
		unauthGroupRouter.GET("rabbitMQ/backExchangeProducer", controllers.RabbitMQController{}.BackExchangeProducer)

		// 应答模式生产者
		unauthGroupRouter.GET("rabbitMQ/ackModeProducer", controllers.RabbitMQController{}.AckModeProducer)

		// 生产者Return模式,消息发送至队列时,队列返回
		unauthGroupRouter.GET("rabbitMQ/returnProducer", controllers.RabbitMQController{}.ReturnProducer)

		// 消息丢失处理机制: 生产者Confirm模式,消息发送至交换机时,交换机返回确认
		unauthGroupRouter.GET("rabbitMQ/confirmProducer", controllers.RabbitMQController{}.ConfirmProducer)

		// 优先级队列
		unauthGroupRouter.GET("rabbitMQ/priorityProducer", controllers.RabbitMQController{}.PriorityProducer)

		// 三叉戟队列
		unauthGroupRouter.GET("rabbitMQ/tridentProducer", controllers.RabbitMQController{}.TridentProducer)
	}

	// 前端v1版接口之非权限路由
	v1FrontendUnauthGroupRouter := r.Group("/api/frontend/v1")
	{
		// 首页
		v1FrontendUnauthGroupRouter.GET("/", v1.IndexController{}.Index)

		// MINIO上传单张图片
		v1FrontendUnauthGroupRouter.GET("/index/minioSingleUploadImage", v1.IndexController{}.MinioSingleUploadImage)

		// 本地上传单张图片
		v1FrontendUnauthGroupRouter.POST("/index/singleUploadImage", v1.IndexController{}.SingleUploadImage)

		// 请求参数案例
		v1FrontendUnauthGroupRouter.POST("/index/testRequestParams", v1.IndexController{}.TestRequestParams)

		// ------------------------ 用户路由开始 ------------------------
		// 用户注册
		v1FrontendUnauthGroupRouter.POST("user/register", v1.UserController{}.Register)

		// 用户登录
		v1FrontendUnauthGroupRouter.POST("user/login", v1.UserController{}.Login)
		// ------------------------ 用户路由结束 ------------------------

		// ------------------------ 横幅路由开始 ------------------------
		// 列表
		v1FrontendUnauthGroupRouter.GET("banner/list", v1.BannerController{}.List)
		// 详情
		v1FrontendUnauthGroupRouter.GET("banner/detail", v1.BannerController{}.Detail)
		// ------------------------ 横幅路由结束 ------------------------
	}

	// 前端v1版接口之权限路由
	v1FrontendAuthGroupRouter := r.Group("/api/frontend/v1")
	v1FrontendAuthGroupRouter.Use(middlewares.FrontendAuthMiddleware)
	{
		// ------------------------ 用户路由开始 ------------------------
		// 详情
		v1FrontendAuthGroupRouter.GET("user/detail", v1.UserController{}.Detail)
		// ------------------------ 用户路由结束 ------------------------
	}

	// 前端接口之非权限路由
	v2FrontendUnauthGroupRouter := r.Group("/api/frontend/v2")
	{
		// 首页
		v2FrontendUnauthGroupRouter.GET("/", v2.IndexController{}.Index)
	}

	// -------------------------------------------------------------------------------------
	// 管理v1版接口之非权限路由
	v1MgrUnauthGroupRouter := r.Group("/api/mgr/v1")
	{
		// ------------------------ 账户路由开始 ------------------------
		// 登录
		v1MgrUnauthGroupRouter.POST("account/login", v1.AccountController{}.Login)

		// ------------------------ 账户路由结束 ------------------------
	}

	// 管理v1版接口之权限路由
	v1MgrAuthGroupRouter := r.Group("/api/mgr/v1")
	v1MgrAuthGroupRouter.Use(middlewares.MgrAuthMiddleware)
	{
		// ------------------------ 横幅路由开始 ------------------------
		// 新增
		v1MgrAuthGroupRouter.POST("banner/add", v1.BannerController{}.Add)

		// ------------------------ 横幅路由结束 ------------------------

		// ------------------------ 账户路由开始 ------------------------
		// 新增
		v1MgrAuthGroupRouter.POST("account/add", v1.AccountController{}.Add)

		// 删除
		v1MgrAuthGroupRouter.POST("account/delete", v1.AccountController{}.Delete)

		// 编辑
		v1MgrAuthGroupRouter.POST("account/edit", v1.AccountController{}.Edit)

		// 编辑状态
		v1MgrAuthGroupRouter.GET("account/editStatus", v1.AccountController{}.EditStatus)

		// 列表
		v1MgrAuthGroupRouter.GET("account/list", v1.AccountController{}.List)

		// ------------------------ 账户路由结束 ------------------------
	}
}
